<template>
  <div class="app-container">
    <el-card class="box-card"  style="border-radius: 10px;"  v-show="showSearch">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="元器件类型" prop="componentType">
        <el-select
          v-model="queryParams.componentType"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option
            v-for="(item, index) in componentTypeList"
            :value="item.dictValue"
            :label="item.dictLabel"
            :key="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select
          v-model="queryParams.dataType"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option
            v-for="(item, index) in dataTypeEnum"
            :label="item.name"
            :value="item.value"
            :key="index"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="序号" prop="priority">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.priority"-->
      <!--          placeholder="请输入序号"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="电路模板编号" prop="circuitId">
        <el-input
          v-model="queryParams.circuitId"
          placeholder="请输入电路模板编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="输入最小值" prop="inputMinValue">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.inputMinValue"-->
      <!--          placeholder="请输入输入最小值"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="输入最大值" prop="inputMaxValue">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.inputMaxValue"-->
      <!--          placeholder="请输入输入最大值"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    </el-card>

    <el-card class="box-card margin_t10"  style="border-radius: 10px;" >

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="paramList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="序号" align="center" prop="priority" />-->
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <!--      <el-table-column label="结构类型" align="center" >-->
      <!--        <template v-if="scope.row.structType" slot-scope="scope">-->
      <!--          <span>{{scope.row.structType === 1 ? '整型' : scope.row.structType === 2 ? '浮点型' : '字符串'}}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="数据类型" align="center">
        <template v-if="scope.row.dataType" slot-scope="scope">
          <span>{{ GlobalVariable.getOneDataType(scope.row.dataType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="元器件类型" align="center" prop="componentType">
        <template v-if="scope.row.componentType" slot-scope="scope">
          <span>{{ getOneComponentType(scope.row.componentType) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="电路模板编号" align="center" prop="circuitId" />
      <el-table-column label="电路模板名称" align="center" prop="circuitName" />
      <!--      <el-table-column label="输入最小值" align="center" prop="inputMinValue" />-->
      <!--      <el-table-column label="输入最大值" align="center" prop="inputMaxValue" />-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-card>

    <!-- 添加或修改输出场景参数对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <!--        <el-form-item label="结构类型" prop="structType">-->
        <!--          <el-select v-model="form.structType" placeholder="请选择类型">-->
        <!--            <el-option label="整型" :value="1" >整型</el-option>-->
        <!--            <el-option label="浮点型" :value="2" >浮点型</el-option>-->
        <!--            <el-option label="字符串" :value="3" >字符串</el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择类型">
            <el-option
              v-for="(item, index) of dataTypeEnum"
              :key="index"
              :value="item.value"
              :label="item.name"
              >{{ item.name }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="元器件类型" prop="componentType">
          <el-select v-model="form.componentType" placeholder="请选择类型">
            <el-option
              v-for="(item, index) of componentTypeList"
              :key="index"
              :value="item.dictValue"
              :label="item.dictLabel"
              >{{ item.dictLabel }}</el-option
            >
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="序号" prop="priority">-->
        <!--          <el-input v-model="form.priority" placeholder="请输入序号" />-->
        <!--        </el-form-item>-->
        <el-form-item label="电路模板编号" prop="circuitId">
          <el-input v-model="form.circuitId" placeholder="请输入电路模板编号" />
        </el-form-item>
<!--        <el-form-item label="电路模板名称" >-->
<!--          <el-select v-model="form.circuitId" filterable placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in options"-->
<!--              :key="item.id"-->
<!--              :label="item.name"-->
<!--              :value="item.id"-->
<!--            >-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <!--        <el-form-item label="输入最小值" prop="inputMinValue">-->
        <!--          <el-input v-model="form.inputMinValue" placeholder="请输入输入最小值" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="输入最大值" prop="inputMaxValue">-->
        <!--          <el-input v-model="form.inputMaxValue" placeholder="请输入输入最大值" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listParam,
  getParam,
  delParam,
  addParam,
  updateParam,
} from "@/api/expert/param";
import { allListCircuit } from "@/api/expert/circuit";
import { getDicts } from "@/api/system/dict/data";
export default {
  name: "Param",
  data() {
    return {
      //修改时获取所有电路列表
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 输出场景参表格数据
      paramList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        priority: null,
        circuitId: null,
        inputMinValue: null,
        inputMaxValue: null,
        code: null,
        dataType: null,
        componentType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // ------------------------------------------------------------------------------------------
      // 数据类型枚举
      dataTypeEnum: this.GlobalVariable.dataTypeEnum,
      // 元器件类型
      componentTypeList: [],
    };
  },
  created() {
    this.getList();
    this.getDicts();
  },
  methods: {
    /** 查询电路列表 */
    getAllListCircuit() {
      allListCircuit().then((response) => {
        const arr = response.data;
        var arr2 = [];
        arr.forEach(element => {
          var obj = {};
          obj.id = element.id;
          obj.name = element.name;
          arr2.push(obj);
        });
        this.options = arr2;
      });
    },
    /** 查询输出场景参数列表 */
    getList() {
      this.loading = true;
      listParam(this.queryParams).then((response) => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询元器件类型列表 */
    getDicts() {
      getDicts("expert_component_type").then((res) => {
        this.componentTypeList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        structType: null,
        dataType: null,
        priority: null,
        circuitId: null,
        inputMinValue: null,
        inputMaxValue: null,
        code: null,
        componentType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加输出场景参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getParam(id).then((response) => {
        this.form = response.data;

        this.form.componentType = this.form.componentType.toString();

        this.open = true;
        this.title = "修改输出场景参数";
      });
      // this.getAllListCircuit();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateParam(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParam(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除输出场景参数编号为"' + ids + '"的数据项？')
        .then(function () {
          return delParam(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "edsta/param/export",
        {
          ...this.queryParams,
        },
        `edsta_param.xlsx`
      );
    },

    //------------------------------------------------------------------------

    // 获取单个元器件类型
    getOneComponentType(val) {
      for (let i = 0; i < this.componentTypeList.length; i++) {
        if (val == this.componentTypeList[i].dictValue) {
          return this.componentTypeList[i].dictLabel;
        }
      }
    },
  },
};
</script>
